package com.sangmo.fts.teacher.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.teacher.interfaces.ScoreSettingCreation;
import com.sangmo.fts.teacher.interfaces.ScoreSettingUpdate;
import com.sangmo.fts.teacher.model.ScoreSetting;
import com.sangmo.fts.teacher.repo.ScoreSettingRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ScoreSettingEntityService extends BaseViewService<ScoreSetting> {

    @Autowired
    private ScoreSettingRepo  scoreSettingRepo;

    @Autowired
    private GrowthSettingEntityService growthSettingEntityService;

    @Autowired
    private DurationRatioSettingEntityService durationRatioSettingEntityService;

    @Override
    protected IDataViewRepo<ScoreSetting> getRepo() {
        return scoreSettingRepo;
    }

    @Override
    protected void onCreate(ScoreSetting entity, Object bo) {
        super.onCreate(entity, bo);
        checkDuplicated((Long)UserSession.getUserInterface().getId());
        ScoreSettingCreation creation = (ScoreSettingCreation)bo;
        checkRatioValid(creation.getDurationRatio(), creation.getGrowthRatio(),
                creation.getProcessRatio(), creation.getReportRatio());
    }

    @Override
    protected void onUpdate(ScoreSetting entity, Object bo) {
        super.onUpdate(entity, bo);
        ScoreSettingUpdate update = (ScoreSettingUpdate) bo;
        checkRatioValid(update.getDurationRatio(), update.getGrowthRatio(),
                update.getProcessRatio(), update.getReportRatio());
    }

    @Override
    protected void afterCreated(ScoreSetting entity, Object bo) {
        super.afterCreated(entity, bo);
        ScoreSettingCreation creation = (ScoreSettingCreation)bo;
        growthSettingEntityService.create(entity.getId(), creation.getGrowthRatioSettings());
        durationRatioSettingEntityService.create(entity.getId(), creation.getDurationRatioSettings());
    }

    @Override
    protected void afterUpdated(ScoreSetting entity, Object bo) {
        super.afterUpdated(entity, bo);
        ScoreSettingUpdate update = (ScoreSettingUpdate) bo;
        growthSettingEntityService.deleteByScoreSettingId(entity.getId());
        durationRatioSettingEntityService.deleteByScoreSettingId(entity.getId());

        growthSettingEntityService.create(entity.getId(), update.getGrowthRatioSettings());
        durationRatioSettingEntityService.create(entity.getId(), update.getDurationRatioSettings());
    }


    private void checkRatioValid(int durationRatio, int growthRatio, int processRatio, int reportRatio) {
        int total = durationRatio + growthRatio
                + processRatio + reportRatio;
        if (100 != total) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.scoreSetting.ratio.notValid");
        }
    }

    protected  ScoreSetting findByUserId(Long createdBy) {
        return scoreSettingRepo.findFirstByUserId(createdBy);
    }

    private void checkDuplicated(Long userId) {
        ScoreSetting ss = this.findByUserId(userId);
        if (null != ss) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.rpfPledgeContract.register.duplicate");
        }
    }
}
